﻿namespace PreGUI.Widgets
{
    using PreGUI.Interfaces;
    using UnityEngine;
    using UnityEngine.UI;
    using PreGUI.Helpers;

    /// <summary>
    /// PreGui Button Control
    /// </summary>
    public class PGButton : IPGControl
    {
        public string ControlName = "";
        public string Caption
        {
            get { return _caption; }
            set
            {
                _caption = value;
                _textComponent.text = value;
            }
        }
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                _cursorListener.Enabled = value;
                _enabled = value;
                _buttonComponent.enabled = value;
            }
        }
        public System.Action OnClick;

        private bool _enabled = true;
        private string _caption;

        private PGCursorListener _cursorListener;
        private Button _buttonComponent;
        private Text _textComponent;
        public GameObject LayoutControls(PreGui gui, GameObject parent, string windowName)
        {
            var gameObject = new GameObject("PGButton");
            gameObject.transform.SetParent(parent.transform);

            _cursorListener = gameObject.AddComponent<PGCursorListener>();
            _cursorListener.Gui = gui;
            _cursorListener.CursorStyle = eCursorMode.Pointer;

            if (gui.ViewModels.ContainsKey(windowName))
                this.BindTo(ControlName, gui.ViewModels[windowName]);

            var image = gameObject.AddComponent<Image>();
            image.color = gui.ButtonSettings.BackgroundColor;
            image.sprite = gui.ControlSprite;
            image.type = Image.Type.Sliced;

            _buttonComponent = gameObject.AddComponent<Button>();
            _buttonComponent.enabled = _enabled;
            _buttonComponent.colors = new ColorBlock
            {
                normalColor = gui.ButtonSettings.BackgroundColor,
                highlightedColor = gui.ButtonSettings.HighlightColor,
                pressedColor = gui.ButtonSettings.PressedColor,
                disabledColor = gui.ButtonSettings.DisabledColor,
                colorMultiplier = 1.0f,
                fadeDuration = 0.005f
            };

            _buttonComponent.onClick.AddListener(() =>
            {
                if (gui.FunctionHandlers.ContainsKey(ControlName + ".Clicked"))
                    gui.FunctionHandlers[ControlName + ".Clicked"](this);

                if (gui.ViewModels.ContainsKey(windowName))
                    gui.ViewModels[windowName].CallEventAction<PGButton>(ControlName, this);

            });

            if (OnClick != null)
            {
                _buttonComponent.onClick.AddListener(() => { OnClick(); });
            }

            var textObject = new GameObject("Text");
            textObject.transform.SetParent(gameObject.transform);

            var rectTextTransform = textObject.AddComponent<RectTransform>();
            rectTextTransform.anchorMin = new Vector2(0f, 0f);
            rectTextTransform.anchorMax = new Vector2(1f, 1f);
            rectTextTransform.position = gameObject.transform.position;

            rectTextTransform.offsetMin = new Vector2(2f, 4f);
            rectTextTransform.offsetMax = new Vector2(0f, 0f);

            _textComponent = textObject.AddComponent<Text>();
            _textComponent.text = _caption;
            _textComponent.font = gui.ButtonSettings.Font;
            _textComponent.color = gui.ButtonSettings.FontColor;
            _textComponent.fontSize = gui.ButtonSettings.FontSize;
            _textComponent.fontStyle = gui.ButtonSettings.FontStyle;
            _textComponent.alignment = TextAnchor.MiddleCenter;

            var layout = gameObject.AddComponent<LayoutElement>();
            layout.minHeight = gui.ButtonSettings.MinimumHeight;
            layout.minWidth = gui.ButtonSettings.MinimumWidth;

            return gameObject;
        }


        public IPGControl GenerateFromXmlNode(System.Xml.XmlNode node)
        {

            if (node.Attributes["Name"] != null)
                ControlName = node.Attributes["Name"].Value;

            _caption = node.InnerText.Trim();

            if (node.Attributes["Enabled"] != null)
                _enabled = System.Convert.ToBoolean(node.Attributes["Enabled"].Value);

            return this;
        }
    }
}

